package com.jx.tool.utils;

import java.io.File;
import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

public class XmlUtils {

	public static <T> T xmlToObject(InputStream is, Class<T> classOfT)
			throws JAXBException {
		JAXBContext jc = JAXBContext.newInstance(classOfT);
		Unmarshaller um = jc.createUnmarshaller();
		JAXBElement<T> je = um.unmarshal(new StreamSource(is), classOfT);
		return je.getValue();
	}
	
	public static <T> T xmlToObject(File file, Class<T> classOfT) throws JAXBException {
		JAXBContext jc = JAXBContext.newInstance(classOfT);
		Unmarshaller um = jc.createUnmarshaller();
		JAXBElement<T> je = um.unmarshal(new StreamSource(file), classOfT);
		return je.getValue();
	}

}
